package com.cimp.util.common.log;

import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * [简要描述]:
 * [详细描述]:
 *
 * @author huojinhao
 * @date 2018/11/16
 */
public class StreamGobbler extends Thread{
    public Logger logger = Logger.getLogger(StreamGobbler.class);

    private InputStream is;

    private StreamType streamType;

    public StreamGobbler(InputStream is, StreamType streamType) {
        this.is = is;
        this.streamType = streamType;
    }

    public void run() {
        try {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                switch (streamType) {
                    case INFO:
                        logger.info(line);
                        break;
                    case DEBUG:
                        logger.debug(line);
                        break;
                    case WARN:
                        logger.warn(line);
                        break;
                    case ERROR:
                        logger.error(line);
                        break;
                }

            }
        } catch (IOException ioe) {
            logger.error("StreamGobbler IOException ",ioe);
        }
    }
}
